class_name Player extends CharacterBody2D

const move_speed: float = 5.0
var screen_size: Vector2
var minPosition= Vector2.ZERO
var maxPosition: Vector2
var currState: State= null
var orientation: String = "down"
var count: float= 0

signal hit(i:int)

func _ready() -> void:
	screen_size= get_viewport_rect().size
	maxPosition = screen_size
	minPosition.x += 24
	minPosition.y += 24
	maxPosition.x -= 24
	maxPosition.y -= 24
	pass

func _process(delta: float) -> void:
	
	# 检测输入
	if Input.is_action_pressed("atk"):
		$playerStateMachine.updateState(self,"atk")
		hit.emit(6)
		return
	
	var vector: Vector2 = Vector2.ZERO
	if Input.is_action_pressed("move_right"):
		vector.x += 48
	if Input.is_action_pressed("move_left"):
		vector.x -= 48
	if Input.is_action_pressed("move_down"):
		vector.y += 48
	if Input.is_action_pressed("move_up"):
		vector.y -= 48
	
	# 设置位移，防止出界
	position += vector * delta * move_speed
	position = position.clamp(minPosition, maxPosition)
	
	#角色当前的状态
	if vector.x != 0:
		if vector.x < 0: orientation="left"
		else: orientation="right"
		$playerStateMachine.updateState(self,"walk")
	elif vector.y != 0:
		if vector.y < 0: orientation ="up"
		else: orientation ="down"
		$playerStateMachine.updateState(self,"walk")
	else:
		# 没有输入时，返回空闲状态
		$playerStateMachine.updateState(self,"idle")
	pass

func _physics_process(_delta: float) -> void:
	move_and_slide()
	pass

func getOrientation()->String:return orientation

func setSate(state:String) -> void:
	var as2d=$AnimatedSprite2D
	
	if "idle" == state :
		if "down" == orientation:
			as2d.animation = "idle_down"
			as2d.flip_v = false
			as2d.flip_h = false
		elif "up" == orientation :
			as2d.animation = "idle_up"
			as2d.flip_v = false
			as2d.flip_h = false
		elif "right" == orientation :
			as2d.animation = "idle_right"
			as2d.flip_v = false
			as2d.flip_h = false
		elif "left" == orientation :
			as2d.animation = "idle_right"
			as2d.flip_v = false
			as2d.flip_h = true
	elif "walk" == state :
		if "down" == orientation:
			$AnimatedSprite2D.animation = "walk_down"
			$AnimatedSprite2D.flip_v = false
			$AnimatedSprite2D.flip_h = false
		elif "up" == orientation :
			$AnimatedSprite2D.animation = "walk_up"
			$AnimatedSprite2D.flip_v = false
			$AnimatedSprite2D.flip_h = false
		elif "right" == orientation :
			$AnimatedSprite2D.animation = "walk_right"
			$AnimatedSprite2D.flip_v = false
			$AnimatedSprite2D.flip_h = false
		elif "left" == orientation :
			$AnimatedSprite2D.animation = "walk_right"
			$AnimatedSprite2D.flip_v = false
			$AnimatedSprite2D.flip_h = true
	elif "atk" == state : 
		if "up" == orientation:
			$AnimatedSprite2D.animation = "atk_up"
			$AnimatedSprite2D.flip_h = false
		elif "down" == orientation : 
			$AnimatedSprite2D.animation = "atk_down"
			$AnimatedSprite2D.flip_h = false
		elif "left" == orientation : 
			$AnimatedSprite2D.animation = "atk_right"
			$AnimatedSprite2D.flip_h = true
		elif "right" == orientation : 
			$AnimatedSprite2D.animation = "atk_right"
			$AnimatedSprite2D.flip_h = false
	pass

func buildAnimeSprite(config:Dictionary)->void:
	var path:String= config.get("path")
	var actionImg= Image.load_from_file(path)
	var actionList:Dictionary= config.get("actionList")
	var spriteFrames:SpriteFrames= $AnimatedSprite2D.sprite_frames
	for actionName:String in actionList:
		var actionArray:Array[Dictionary]= actionList[actionName]
		spriteFrames.add_animation(actionName)
		for frame in actionArray:
			var x:int= frame.get("x")
			var y:int= frame.get("y")
			var w:int= frame.get("w")
			var h:int= frame.get("h")
			var img= TextureUtils.get_region(actionImg, Rect2i(x,y,w,h))
			spriteFrames.add_frame(actionName,img)
	pass
